#!/bin/bash
# zeping lai
# www.linxhub.org
# /etc/init.d/sockd
# chmod +x /etc/init.d/sockd 

### BEGIN INIT INFO
# Provides:          sockd
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the sockd  sockd5 server
# Description:       starts sockd using start-stop-daemon
### END INIT INFO

NAME=sockd
SOCKED_BIN=/usr/local/dante/sbin/sockd
CONFIGFILE=/etc/sockd.conf
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
	start)
		echo -n "Starting $NAME...."
		if netstat -tnpl | grep -q sockd;then
		echo "$NAME (pid `pidof $NAME`) already running."
		exit 1
		fi
		$SOCKED_BIN -f $CONFIGFILE -D

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Stoping $NAME..."
		if ! netstat -tnpl | grep -q sockd; then
			echo "$NAME is not running."
			exit 1
		fi

		/usr/bin/pkill $NAME

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if netstat -tnpl | grep -q sockd; then
			PID=`pidof sockd`
			echo "$NAME (pid $PID) is running..."
		else
			echo "$NAME is stopped"
			exit 0
		fi
	;;

	restart)
	        $SCRIPTNAME stop
                sleep 1
                $SCRIPTNAME start
        ;;

	 *)
                echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
                exit 1
        ;;
esac
